<template>
  <base-dialog @cancel="cancelDialog">
    <div slot="header">{{$t('cloudenv.text_480')}}</div>
    <div slot="body">
      <a-form
        :form="form.fc">
        <a-form-item :label="$t('cloudenv.text_95')" v-bind="formItemLayout">
          <a-input v-decorator="decorators.name" :placeholder="$t('cloudenv.text_190')" />
        </a-form-item>
        <a-form-item :label="$t('cloudenv.text_10')" v-bind="formItemLayout">
          <base-select
            resource="cloudregions"
            v-decorator="decorators.region"
            :selectProps="{ 'placeholder': $t('cloudenv.text_231') }"
            :params="{ 'cloud_env': 'private_or_onpremise', 'brand': 'OneCloud' }" />
        </a-form-item>
        <a-form-item :label="$t('cloudenv.text_374')" v-bind="formItemLayout">
          <a-input v-decorator="decorators.location" :placeholder="$t('cloudenv.text_481')" />
        </a-form-item>
      </a-form>
    </div>
    <div slot="footer">
      <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
      <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
    </div>
  </base-dialog>
</template>

<script>
import DialogMixin from '@/mixins/dialog'
import WindowsMixin from '@/mixins/windows'

export default {
  name: 'CreateZoneDialog',
  mixins: [DialogMixin, WindowsMixin],
  data () {
    return {
      loading: false,
      form: {
        fc: this.$form.createForm(this),
      },
      decorators: {
        name: [
          'name',
          {
            validateFirst: true,
            rules: [
              { required: true, message: this.$t('cloudenv.text_190') },
              // { validator: this.$validate('resourceName') },
            ],
          },
        ],
        region: [
          'region',
          {
            rules: [
              { required: true, message: this.$t('cloudenv.text_231') },
            ],
          },
        ],
        location: [
          'location',
          {
            rules: [
              { required: true, message: this.$t('cloudenv.text_482') },
            ],
          },
        ],
      },
      formItemLayout: {
        wrapperCol: {
          span: 21,
        },
        labelCol: {
          span: 3,
        },
      },
    }
  },
  methods: {
    validateForm () {
      return new Promise((resolve, reject) => {
        this.form.fc.validateFields((err, values) => {
          if (!err) {
            resolve(values)
          } else {
            reject(err)
          }
        })
      })
    },
    doCreate (data) {
      return this.params.onManager('create', {
        managerArgs: {
          data,
        },
      })
    },
    async handleConfirm () {
      this.loading = true
      try {
        const values = await this.validateForm()
        this.loading = true
        await this.doCreate(values)
        this.loading = false
        this.cancelDialog()
      } catch (error) {
        this.loading = false
      }
    },
  },
}
</script>
